﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace WindowsFormsApplication1
{
    static class UpdateWinForm
    {
        static public int FreqUpdate = 500; //ms
        //private static System.Windows.Forms.DataGridView MyDataGridView;
        private static PricesData myPricesData;
        private static refreshDef MyrefreshDel;

        public static void setTheDataGridView(PricesData thePricesData)
        {
            myPricesData = thePricesData;
            MyrefreshDel = myPricesData.MyDataGridView.Refresh;
        }

        public static void UpdateLoop()
        {
            while (true)
            {
                Feed.myEventWaitHandle.WaitOne(); 
                System.Threading.Thread.Sleep(FreqUpdate);
                
                Console.WriteLine("Update DataGrid");

                lock (myPricesData.MyDataDico_ChangedRows)
                {
                    for (int i = 0; i < myPricesData.MyDataGridView.Rows.Count; i++)
                    {
                        if (myPricesData.MyDataDico_ChangedRows.Contains(i))
                        {
                            myPricesData.MyDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Red; 
                        }
                        else
                        {
                            myPricesData.MyDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.White; 
                        }
                        
                    }
                    myPricesData.MyDataDico_ChangedRows.Clear();

                    myPricesData.MyDataGridView.Invoke(MyrefreshDel);
                }

               
            }
        }
    }
}
